/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.managers;

import java.io.File;
import java.util.ResourceBundle;
import java.util.logging.Level;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import project.jpdftools.managers.actionlisteners.ApplyLocalizationAction;
import project.jpdftools.managers.other.FileUtil;

/**
 *
 * @author Mido
 */
public class LocalizationManager {

    private static ResourceBundle currentResource; // Resource bundle to hold the current language
    private JMenu menu; // the menu that will be used to add the components in
    // langauges radio buttons
    // must be static to be able to use them while initializing the program
    private static final JRadioButtonMenuItem rbmiLanguageArabic = new JRadioButtonMenuItem("Arabic (Egypt)");
    private static final JRadioButtonMenuItem rbmiLanguageEnglish = new JRadioButtonMenuItem("English (United States)");
    private static final ButtonGroup buttonGroup = new ButtonGroup();

    // constructor with parameters
    public LocalizationManager(JMenu menu) {
        this.menu = menu;
        // intialize the button group components
        buttonGroup.add(rbmiLanguageArabic);
        buttonGroup.add(rbmiLanguageEnglish);
    }

    /**
     * Return the current resource bundle
     *
     * @return
     */
    public static ResourceBundle getCurrent_resource() {
        return currentResource;
    }

    /**
     * used to initialize the locale of the program
     */
    public static void intializeLocalization() {
        File locale_file = new File("Data/Language.txt");
        // check if the file already exists
        if (locale_file.exists() == true) {
            String read_locale = new FileUtil().readFromFile("Data/Language.txt");
            // make sure that the file is not empty
            if (read_locale != null) {
                currentResource = ResourceBundle.getBundle(read_locale);
                // select/deselect language radio buttons
                selectLanguageInIntialization(read_locale);
                LoggerManager.getLogger().log(Level.INFO, "Selected Locale \"{0}\"", read_locale);
            } else {
                // set default language
                saveCurrentResource("project.jpdftools.localization.en_US");
                // tell the user that the file is empty
                JOptionPane.showMessageDialog(null, "Language file is empty. Default language 'English US' is selected", "Empty!", 0);
                // select/deselect language radio buttons
                selectLanguageInIntialization("project.jpdftools.localization.en_US");
                LoggerManager.getLogger().warning("Locale file is Empty. Default Locale Selected \"project.jpdftools.localization.en_US\"");
            }
        } else {
            // set default language
            saveCurrentResource("project.jpdftools.localization.en_US");
            // tell the user about the absence of the file
            JOptionPane.showMessageDialog(null, "Language file is missing. Default language 'English US' is selected", "Missing!", 0);
            // select/deselect language radio buttons
            selectLanguageInIntialization("project.jpdftools.localization.en_US");
            LoggerManager.getLogger().warning("Locale file not found. Default Locale Selected \"project.jpdftools.localization.en_US\"");
        }
    }

    /**
     * Selects the radio button of the saved language
     *
     * @param language
     */
    private static void selectLanguageInIntialization(String language) {
        if (language.equals("project.jpdftools.localization.ar_EG") == true) {
            rbmiLanguageArabic.setSelected(true);
        } else if (language.equals("project.jpdftools.localization.en_US") == true) {
            rbmiLanguageEnglish.setSelected(true);
        }
    }

    /**
     * Saves the current resource to an external file
     *
     * @param current_resource
     */
    public static void saveCurrentResource(String current_resource) {
        LocalizationManager.currentResource = ResourceBundle.getBundle(current_resource);
        // save localization in a file for future uses
        new FileUtil().writeToFile("Data/Language.txt", current_resource, false, false);
        LoggerManager.getLogger().log(Level.INFO, "Locale changed to \"{0}\"", current_resource);
    }

    /**
     * Applies the localization in the UI
     */
    public void applyLocaliztionInUI() {
        // add action listeners
        rbmiLanguageArabic.addActionListener(new ApplyLocalizationAction("project.jpdftools.localization.ar_EG"));
        rbmiLanguageEnglish.addActionListener(new ApplyLocalizationAction("project.jpdftools.localization.en_US"));

        // add radio button components to the menu
        menu.add(rbmiLanguageArabic);
        menu.add(rbmiLanguageEnglish);
    }
}
